﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using System.Diagnostics;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace bgp8.Controls
{
    public sealed partial class TopAppBarPanel : UserControl
    {

        #region events
        public delegate void TopAppBarButtonClickedHandler(Object sender, GamePieceEventArgs e);
        public event TopAppBarButtonClickedHandler TopAppBarButtonClicked;

        private void OnTopAppBarButtonClicked(GamePieceEventArgs e) 
        {
            TopAppBarButtonClicked(this, e);
        }
        #endregion

        public TopAppBarPanel()
        {
            this.InitializeComponent();
            SetValue(DiceVisibilityProperty, Visibility.Collapsed);
            SetValue(TokenVisibilityProperty, Visibility.Collapsed);
            Loaded += TopAppBarPanel_Loaded;
        }

        void TopAppBarPanel_Loaded(object sender, RoutedEventArgs e)
        {
            createTokens();
            TokenVisibility = Visibility.Visible;
        }

        public static DependencyProperty DiceVisibilityProperty = DependencyProperty.Register("DiceVisibility", typeof(Visibility), typeof(TopAppBarPanel), null);
        public static DependencyProperty TokenVisibilityProperty = DependencyProperty.Register("TokenVisibility", typeof(Visibility), typeof(TopAppBarPanel), null);

        public Visibility DiceVisibility 
        {
            get { return (Visibility)GetValue(DiceVisibilityProperty); }
            set 
            {
                SetValue(DiceVisibilityProperty, value);
                if (DiceVisibility.Equals(TokenVisibility) && DiceVisibility.Equals(Visibility.Visible)) 
                {
                    TokenVisibility = Visibility.Collapsed;
                }
            }
        }

        public Visibility TokenVisibility 
        {
            get { return (Visibility)GetValue(TokenVisibilityProperty); }
            set
            {
                SetValue(TokenVisibilityProperty, value);
                if (TokenVisibility.Equals(DiceVisibility) && TokenVisibility.Equals(Visibility.Visible))
                {
                    DiceVisibility = Visibility.Collapsed;
                }
            }
        }

        private void createTokens() 
        {
            DiceStackPanel.Children.Clear();
            TokenStackPanel.Children.Clear();
            IEnumerable<Object> gp = this.Resources.Values;
            foreach (Object o in gp)
            {
                GamePiece piece = o as GamePiece;
                if (piece.GetType() == typeof(Die)) 
                {
                    DiceStackPanel.Children.Add(MakeGamePieceButton(piece));
                }
                if (piece.GetType() == typeof(Token)) 
                {
                    TokenStackPanel.Children.Add(MakeGamePieceButton(piece));
                }
            }
        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            GamePiece piece = button.Tag as GamePiece;
            GamePieceEventArgs args = new GamePieceEventArgs(piece);
            OnTopAppBarButtonClicked(args);
        }

        private Panel MakeGamePieceButton(GamePiece piece) 
        {
            GamePiece newPiece = piece.Clone();
            Button button = new Button();
            button.Width = piece.Width;
            button.Height = piece.Height;
            button.Click += button_Click;
            button.Tag = newPiece;
            Grid grid = new Grid();
            grid.Children.Add(newPiece);
            grid.Children.Add(button);
            return grid;
        }

        private void DiceToggleButtonClick(object sender, RoutedEventArgs e)
        {
            DiceVisibility = Visibility.Visible;
        }

        private void TokenToggleButtonClick(object sender, RoutedEventArgs e) 
        {
            TokenVisibility = Visibility.Visible;
        }




        


    }
}
